package com.xin.minibusmall.product.service.impl;

import com.mysql.cj.util.StringUtils;
import com.xin.common.constant.ProductConstant;
import com.xin.minibusmall.product.dao.AttrAttrgroupRelationDao;
import com.xin.minibusmall.product.dao.AttrDao;
import com.xin.minibusmall.product.entity.AttrAttrgroupRelationEntity;
import com.xin.minibusmall.product.entity.AttrEntity;
import com.xin.minibusmall.product.service.AttrService;
import com.xin.minibusmall.product.vo.AttrGroupWithAttrsResVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.common.utils.PageUtils;
import com.xin.common.utils.Query;

import com.xin.minibusmall.product.dao.AttrGroupDao;
import com.xin.minibusmall.product.entity.AttrGroupEntity;
import com.xin.minibusmall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Autowired
    AttrDao attrDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        //若key存在，则属性分组的id与key一样或者名字模糊key
        String key = (String) params.get("key");
        if(!StringUtils.isNullOrEmpty(key)){
            wrapper.and(obj->{
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if(catelogId == 0){
            //如果id为0也就是没有指定，则查询全部
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else{
            //id存在，查找对应id的属性分组
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    @Transactional
    public void removeAttrGroups(List<Long> asList) {
        //删除分组
        this.removeByIds(asList);
        //删除对应的关联信息
        attrAttrgroupRelationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", asList));
    }

    /**
     * 根据分类id查出所有分组以及它的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsResVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsResVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsResVo attrGroupWithAttrsResVo = new AttrGroupWithAttrsResVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsResVo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupWithAttrsResVo.getAttrGroupId());
            attrGroupWithAttrsResVo.setAttrs(attrEntities);
            return attrGroupWithAttrsResVo;
        }).collect(Collectors.toList());

        return collect;
    }

}